package main

import (
	"flag"
	"fmt"
	"net"
)

var serverIp string
var serverPort int

func init() {
	flag.StringVar(&serverIp, "ip", "127.0.0.1", "设置服务器IP地址默认为127.0.0.1")
	flag.IntVar(&serverPort, "port", 8888, "设置服务器默认端口为8888")
}

func main() {
	flag.Parse()
	client := NewClient(serverIp, serverPort, "Client1")
	if client == nil {
		fmt.Println("启动失败")
		return
	}

	fmt.Println(">>>>>>client success<<<<<<")
	select {}
}

// Client 客户端
type Client struct {
	serverIp   string
	serverPort int
	Name       string
	conn       net.Conn
}

// 创建client
func NewClient(serverIp string, serverPort int, name string) *Client {
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", serverIp, serverPort))
	if err != nil {
		fmt.Println("net.Dial error", err)
		return nil
	}
	client := &Client{
		serverIp:   serverIp,
		serverPort: serverPort,
		Name:       name,
		conn:       conn}
	return client
}
